<template>
  <section class="author-data">
    <h4 v-if="page === 'article'" class="panel-title">
      关于作者
    </h4>
    <nuxt-link v-if="page === 'article'" class="author-avatar" :to="'/user/' + targetUserId + '?name=' + targetUserData.name">
      <img :src="targetUserData.avatar" alt="">
    </nuxt-link>
    <nuxt-link v-if="page === 'self'" class="author-avatar" :to="'/user/' + targetUserId + '?name=' + targetUserData.name">
      <img :src="targetUserData.avatar" alt="">
    </nuxt-link>
    <div
      v-if="page === 'user'"
      class="title-user"
      :style="'background-image: url(' + targetUserData.background + ')'"
    >
      <image2-base64
        v-if="userData && userData.userId === targetUserId"
        class="btn-background"
        url="user/background"
        icon-class="el-icon-edit"
        @imgUrl="updateUserBackground"
      />
      <nuxt-link class="author-avatar" :to="'/user/' + targetUserId + '?name=' + targetUserData.name">
        <img :src="targetUserData.avatar" alt="">
      </nuxt-link>
    </div>
    <p class="author-name author-info-item">
      <span :title="targetUserData.name">
        {{ targetUserData.name }}
      </span>
    </p>
    <p v-if="['user', 'self'].includes(page)" class="author-intro author-info-item">
      <span :title="authorIntro()">
        {{ authorIntro() }}
      </span>
    </p>
    <p v-if="['user', 'article'].includes(page)" class="author-signature author-info-item">
      <span :title="targetUserData.signature">
        {{ targetUserData.signature }}
      </span>
    </p>
    <a v-if="!userData" class="btn-follow" @click="toFollow('post')">
      关注
    </a>
    <a v-if="userData && targetUserId !== userData.userId" v-show="!targetUserData.follow" class="btn-follow" @click="toFollow('post')">
      关注
    </a>
    <a v-if="userData && targetUserId !== userData.userId" v-show="targetUserData.follow" class="btn-follow haveFollow" @click="toFollow('delete')">
      已关注
    </a>
    <nuxt-link v-if="userData && targetUserId === userData.userId" class="btn-follow" to="/edit">
      写博客
    </nuxt-link>
    <div v-if="['user', 'article'].includes(page)" class="author-record">
      <a class="record-item">
        <p>人气</p>
        <div>{{ targetUserData.rank }}</div>
      </a>
      <a class="record-item">
        <p>文章</p>
        <div>{{ targetUserData.articleCount }}</div>
      </a>
      <a v-if="page === 'user'" class="record-item">
        <p>粉丝</p>
        <div>{{ targetUserData.fansCount }}</div>
      </a>
      <a v-if="page === 'user'" class="record-item">
        <p>关注</p>
        <div>{{ targetUserData.followCount }}</div>
      </a>
    </div>
  </section>
</template>

<script>
import { checkUserData } from '~/assets/js/util'
import image2Base64 from '~/components/public/Image2Base64'

export default {
  name: 'UserPanel',
  components: {
    image2Base64
  },
  props: {
    targetUserId: {
      type: String,
      default: () => ''
    },
    page: {
      type: String,
      default: () => ''
    }
  },
  data() {
    return {
      targetUserData: {}
    }
  },
  computed: {
    userData() {
      return this.$store.state.user.userData
    }
  },
  mounted() {
    // if (this.userData && this.userData.userId === this.targetUserId) {
    //   this.targetUserData = this.userData
    //   return
    // }
    this.getTargetUserData()
  },
  methods: {
    async getTargetUserData() {
      const params = {
        targetUserId: this.targetUserId,
        userId: this.userData ? this.userData.userId : ''
      }
      const data = await this.$request({
        context: this,
        method: 'post',
        url: 'user/userCard',
        params
      })
      if (!data) return
      this.targetUserData = data
    },
    updateUserBackground(data) {
      const newUserData = {
        background: data
      }
      this.$store.commit('updateUserDataMutation', newUserData)
      this.$message.success('修改成功！')
    },
    authorIntro() {
      if (this.targetUserData.address && this.targetUserData.job) {
        return `${this.targetUserData.address} / ${this.targetUserData.job}`
      }
    },
    async toFollow(method) {
      const res = checkUserData(this)
      if (!res) return
      const params = {
        targetUserId: this.targetUserId
      }
      const data = await this.$request({
        context: this,
        method,
        url: 'userFans/fans',
        params
      })
      if (!data) return
      this.targetUserData.follow = !this.targetUserData.follow
    }
  }
}
</script>

<style scoped lang="scss">
@import "~assets/css/public";
.author-data {
  overflow: hidden;
  background: #fff;
  padding-bottom: 40px;

  p.author-name {
    font-size: 20px;
    line-height: 1.2em;
    margin: 20px auto 12px;
    @include one-line-text;
  }

  p.author-intro {
    font-size: 12px;
    line-height: 16px;
    margin: 2px auto;
    color: #999;
    @include one-line-text;
  }

  p.author-signature {
    font-size: 12px;
    margin: 20px auto;
    @include one-line-text;
  }

  p.author-info-item {
    text-align: center;
    padding: 0 40px;
  }

  a.btn-follow {
    cursor: pointer;
    display: block;
    margin: 10px auto;
    width: 72px;
    height: 24px;
    line-height: 24px;
    text-align: center;
    color: #fff;
    background: $theme-color;

    &.haveFollow {
      color: $theme-color;
      background: $theme-color-border;
    }
  }

  .author-record {
    margin: 36px 36px 0;
    display: flex;
    justify-content: space-around;

    a.record-item {
      display: block;

      p {
        font-size: 12px;
        color: #999;
      }
      div, p {
        text-align: center;
      }

    }
  }

  h4.panel-title {
    font-size: 16px;
    height: 18px;
    line-height: 18px;
    position: relative;
    padding-left: 18px;
    margin: 12px 0 14px;

    &:before {
      content: "";
      display: inline-block;
      width: 4px;
      height: 18px;
      background: $theme-color;
      position: absolute;
      top: 0;
      left: 0;
    }
  }

  a.author-avatar {
    display: block;
    width: 84px;
    height: 84px;
    overflow: hidden;
    border-radius: 84px;
    margin: 24px auto 24px;
    img {
      @include inner-img;
    }
  }

  .title-user {
    height: 200px;
    /*background: url("~@/assets/img/UserPanel-back.png") no-repeat;*/
    background-repeat: no-repeat;
    background-size: cover;
    display: flex;
    justify-content: center;
    align-items: flex-end;
    position: relative;
    margin-bottom: 64px;

    .btn-background {
      position: absolute;
      top: 20px;
      right: 20px;
    }
    a.author-avatar {
      display: block;
      width: 124px;
      height: 124px;
      overflow: hidden;
      margin: 0 auto -44px;
      border: 1px solid #fff;
      img {
        @include inner-img;
      }
    }
  }
}
</style>
